package cz.vutbr.fit.dnacompress.alignment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.biojava.bio.Annotation;
import org.biojava.bio.BioException;
import org.biojava.bio.program.ssbind.AnnotationFactory;
import org.biojava.bio.search.SearchContentAdapter;
import org.biojava.bio.seq.DNATools;
import org.biojava.bio.seq.io.SymbolTokenization;
import org.biojava.bio.symbol.*;

import cz.vutbr.fit.dnacompress.Compressor;
import cz.vutbr.fit.dnacompress.events.AlignDeletitionEvent;
import cz.vutbr.fit.dnacompress.events.AlignEvent;
import cz.vutbr.fit.dnacompress.events.AlignInsertionEvent;
import cz.vutbr.fit.dnacompress.events.AlignMutationEvent;


public class FastaSearchContentHandler extends SearchContentAdapter {

    // Data holders for search result properties
    private Map resultPreAnnotation;
    private Map hitData;
    private Map subHitData;

    private List hits;
    private List subHits;
    private Map subHitProperties;
    
    private Map<String, String> sequenceMap;

    // Flag indicating whether there are more results in the stream
    private boolean moreSearchesAvailable = false;
    
    public FastaSearchContentHandler( Map<String, String> sequenceMap)
    {	
        resultPreAnnotation = new HashMap();
        hitData             = new HashMap();
        subHitData          = new HashMap();
        this.sequenceMap = sequenceMap;
    }

	public boolean getMoreSearches()
    {
        return moreSearchesAvailable;
    }

    public void setMoreSearches(boolean value)
    {
        moreSearchesAvailable = value;
    }

    public void startSearch()
    {
        hits = new ArrayList();
    }

    public void endSearch() {}

    public void startHeader()
    {
        resultPreAnnotation.clear();
    }

    public void endHeader() { }

    public void startHit()
    {
        hitData.clear();
        subHits = new ArrayList();
    }

    public void endHit()
    {
        hits.add(makeHit());
    }

    public void startSubHit()
    {
        subHitData.clear();
    }

    public void endSubHit()
    {
        try
        {
            subHits.add(makeSubHit());
        }
        catch (BioException be)
        {
            be.printStackTrace();
        }
    }

    public void addSearchProperty(Object key, Object value)
    {
        resultPreAnnotation.put(key, value);
    }

    public void addHitProperty(Object key, Object value)
    {
        hitData.put(key, value);
    }

    public void addSubHitProperty(Object key, Object value)
    {
        subHitData.put(key, value);
    }


    private Object makeHit()
    {  
    	Annotation an = AnnotationFactory.makeAnnotation(hitData);
    	
        return an;
    }

    private Map makeSubHit() throws BioException
    {
        sequenceMap.putAll( subHitData);
        
        return subHitProperties;
    }
}
